
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../utils/local_storage.dart';

/// 群名片
class ItemSelectWidget extends StatefulWidget {

  List<String> titleArr;
  ValueChanged selectActionValue;
  int selectIndex;

  /// cell 高度
  double itemHeight;
  // cell 宽度
  double itemWidth;

  ItemSelectWidget({
    required this.titleArr,
    required this.selectActionValue,
    required this.selectIndex,
    this.itemHeight = 50,
    this.itemWidth = 400,
  });

  @override
  _ItemSelectWidgetState createState() => _ItemSelectWidgetState();
}

class _ItemSelectWidgetState extends State<ItemSelectWidget> {

  int selectValue = 0;

  bool isDarkMode = LocalStorage.getThemeMode() == 1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    // 初始化数据
    selectValue = widget.selectIndex;
  }

  @override
  Widget build(BuildContext context) {

    return Container(
      height: widget.itemHeight * widget.titleArr.length,
      width: widget.itemWidth,
      // constraints: BoxConstraints(
      //   minHeight: widget.itemHeight * widget.titleArr.length,
      // ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Divider(
            color: Colors.grey.shade200,  // 分割線顏色透明
            height: 1,
            indent: 0,
            endIndent: 0,
            thickness: 1,
          ),
          Expanded(
              child: ListView.separated(
                  itemBuilder: (BuildContext ctx, int index) {
                    return InkWell(
                      onTap: () {
                        setState(() {
                          selectValue = index;
                        });
                        widget.selectActionValue(selectValue);

                      },
                      child: Container(
                        constraints: BoxConstraints(
                          minHeight: 50
                        ),
                        width: widget.itemWidth,
                        padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Icon(Icons.check, color: Colors.transparent,),
                            Container(
                              alignment: Alignment.center,
                              width: widget.itemWidth * 0.5,
                                child: Text("${widget.titleArr[index]}",
                                  style: TextStyle(color: selectValue == index ? Colors.orange : (isDarkMode ? Colors.white : Colors.black), fontSize: 18), textAlign: TextAlign.center,), ),

                            selectValue == index ? Icon(Icons.check, color: Colors.orange,) : Icon(Icons.check, color: Colors.transparent,),
                          ],
                        ),
                      ),
                    );
                  },
                  separatorBuilder: (BuildContext ctx, int index) {
                    return Divider(
                      color: Colors.grey.shade200,  // 分割線顏色透明
                      height: 1,
                      indent: 0,
                      endIndent: 0,
                      thickness: 1,
                    );
                  },
                  itemCount: widget.titleArr.length,
              )
          ),
        ],
      ),
    );
  }
}
